package com.mass.core.sys.file.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mass.core.sys.file.model.FileInfoTemp;
import com.mass.core.sys.file.service.FileUploadService;
import com.mass.core.utils.AjaxResponse;

/**
 * 文件上传控制器 </p>
 *
 * @version 1.0.0
 * @date 2016/11/20 11:36
 */
@Api("文件上传下载控制器")
@RestController
@RequestMapping(value="/file/fileUpload")
public class FileUploadController{

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传文件
     * @param request           服务请求
     * @param response          服务响应
     * @param code              业务编码
     * @param isShrink          是否生成缩列图
     * @return
     */
    @ApiOperation(value = "文件上传", notes = "上传完成后返回文件相对路径和绝对路径")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "file", value = "上传文件", paramType = "form", required = true, dataType = "File"),
    	@ApiImplicitParam(name = "isShrink", value = "图片是否压缩", paramType = "form", required = true, dataType = "boolean"),
    	@ApiImplicitParam(name = "code", value = "文件编码", paramType = "form", required = false, dataType = "String")
    })
    @RequestMapping(value="/uploadFile", method = RequestMethod.POST)
    public AjaxResponse uploadFile(MultipartHttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestParam("isShrink") boolean isShrink,String code) {
        List<FileInfoTemp> list = this.fileUploadService.uploadFile(request, code, isShrink);
        return list == null ? AjaxResponse.error("上传失败",list) : AjaxResponse.success("上传成功",list);
    }
    
    @ApiOperation(value = "文件下载", notes = "根据文件路径下载文件")
    @ApiImplicitParam(name = "filePath", value = "文件绝对路径", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value="/downloadFile", method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response,String filePath){
    	try {
            // path是指欲下载的文件的路径。
            File file = new File(filePath);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            //String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream;charset=UTF-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
